package com.sunjy.common.utils.log;


import org.apache.commons.lang3.StringUtils;

/**
 * Created by TerryYang on 2017/7/3.
 */
public class LogUtil {
    public static enum LogEnum {
        START("开始"), END("结束"), KEY("关键逻辑"), ERROR("错误消息");

        private String value;

        private LogEnum(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }

    public static class LogObj {

        private String key;
        private Object value;

        public String getKey() {
            return key;
        }

        public String getValue() {
            return String.valueOf(value);
        }

        private LogObj(String key, Object value) {
            super();
            this.key = key;
            this.value = value;
        }

        public static LogObj getLog(String key, Object value) {
            return new LogObj(key, value);
        }
    }

    public static String buildLogString(LogEnum logEvent, String message, String errCode, String errMsg, LogObj... args) {
        StringBuilder logString = new StringBuilder();
        buildEventString(logEvent, logString);

        if (!StringUtils.isEmpty(message)) {
            logString.append("\t");
            logString.append("msg:");
            logString.append(message);
        }
        if (!StringUtils.isEmpty(errCode)) {
            logString.append("\t");
            logString.append("errCode:");
            logString.append(errCode);
        }
        if (!StringUtils.isEmpty(errMsg)) {
            logString.append("\t");
            logString.append("errMsg:");
            logString.append(errMsg);
        }
        if (args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                LogObj logObj = args[i];
                if (logObj != null) {
                    logString.append("\t");
                    logString.append(logObj.getKey());
                    logString.append(":");
                    logString.append(logObj.getValue());
                }
            }
        }
        return logString.toString();
    }

    public static String buildLogString(LogEnum logEnum, String message, LogObj... args) {
        return buildLogString(logEnum, message, null, null, args);
    }

    public static String buildLogString(LogEnum logEnum, String message, String errCode, String errMsg, String jsonString) {
        StringBuilder logString = new StringBuilder();
        buildEventString(logEnum, logString);

        if (!StringUtils.isEmpty(message)) {
            logString.append("\t");
            logString.append("msg:");
            logString.append(message);
        }
        if (!StringUtils.isEmpty(errCode)) {
            logString.append("\t");
            logString.append("errCode:");
            logString.append(errCode);
        }
        if (!StringUtils.isEmpty(errMsg)) {
            logString.append("\t");
            logString.append("errMsg:");
            logString.append(errMsg);
        }
        if (!StringUtils.isEmpty(errMsg)) {
            logString.append("\t");
            logString.append("jsonString:");
            logString.append(jsonString);
        }
        return logString.toString();
    }

    public static String buildLogString(LogEnum logEnum, String message, String jsonString) {
        return buildLogString(logEnum, message, null, null, jsonString);
    }


    private static void buildEventString(LogEnum logEnum, StringBuilder logString) {
        logString.append("LogEnum:");
        logString.append(logEnum == null ? "" : logEnum.getValue());
        logString.append("\t");
    }

}
